package edu.mse.camel.client.uml.collab.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import edu.mse.camel.client.uml.collab.loginstate.SessionState;

public class OpenDiagramInCollabEditorAnyRoleAction implements IViewActionDelegate, IEditorActionDelegate {
	
	int lastChoice = -1;
	OpenDiagramInCollabEditorAction source = new OpenDiagramInCollabEditorAction();
	OpenDiagramInCollabEditorAsTargetAction target = new OpenDiagramInCollabEditorAsTargetAction();
	
	@Override
	public void init(IViewPart view) {
		source.init(view);
		target.init(view);
	}
	
	@Override
	public void run(IAction action) {
		if(SessionState.myUserID == null) {
			MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
			messageBox.setText("Create collaboration diagram");
			messageBox.setMessage("Please login first");
			messageBox.open();		
		}
		else if(!SessionState.inSession) {
			MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
			messageBox.setText("Create collaboration diagram");
			messageBox.setMessage("Please join a session first");
			messageBox.open();		
		}
		else {
			if (SessionState.isFacilitator) {
				MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
				messageBox.setText("Create collaboration diagram");
				messageBox.setMessage("You are the facilitator");
				messageBox.open();		

				source.run(action);
			}
			else {
				MessageBox messageBox = new MessageBox(new Shell(), SWT.OK);
				messageBox.setText("Create collaboration diagram");
				messageBox.setMessage("You are NOT the facilitator. Please do not make any change to the diagram or model.");
				messageBox.open();		

				target.run(action);
			}
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// these 2 routines are actually the same, but i need them both to be prepared
		source.selectionChanged(action, selection);
		target.selectionChanged(action, selection);
	}

	@Override
	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		// TODO Auto-generated method stub

	}

}
